<template>
  <el-dialog
      :title="!dataForm.id ? '新增' : '修改'"
      append-to-body
      :close-on-click-modal="false"
      @close="closeDialog()"
      v-model="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="车主姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="车主姓名"></el-input>
      </el-form-item>
      <el-form-item label="车牌类型" prop="carType">
        <el-select v-model="dataForm.carType" clearable class="m-2" placeholder="车牌类型">
          <el-option v-for="item in dicts.car_plate_type" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>
      <el-form-item label="车牌号" prop="plateNumber">
        <el-input v-model="dataForm.plateNumber" placeholder="车牌号"></el-input>
      </el-form-item>
      <el-form-item label="c端用户" prop="frontUserId">
        <el-select v-model="dataForm.frontUserId" clearable class="m-2" placeholder="请选择">
          <el-option v-for="item in dicts.car_plate_type" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime" v-if="dataForm.id">
        <el-input v-model="dataForm.createTime" placeholder="创建时间" disabled></el-input>
      </el-form-item>
      <el-form-item label="创建人" prop="createBy" v-if="dataForm.id">
        <el-input v-model="dataForm.createBy" placeholder="创建人" disabled></el-input>
      </el-form-item>
      <el-form-item label="修改时间" prop="updateTime" v-if="dataForm.id">
        <el-input v-model="dataForm.updateTime" placeholder="修改时间" disabled></el-input>
      </el-form-item>
      <el-form-item label="修改人" prop="updateBy" v-if="dataForm.id">
        <el-input v-model="dataForm.updateBy" placeholder="修改人" disabled></el-input>
      </el-form-item>
      <el-form-item label="车辆照片" prop="img">
        <ImageUpload v-model="dataForm.img"></ImageUpload>
      </el-form-item>
      <el-form-item label="信息来源" prop="dataSourceType">
        <el-select v-model="dataForm.dataSourceType" clearable class="m-2" placeholder="信息来源">
          <el-option v-for="item in dicts.car_owner_source" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="收费员id" prop="collectorId">
        <el-input v-model="dataForm.collectorId" placeholder="收费员id"></el-input>
      </el-form-item>-->
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import {getObj, addObj, putObj} from '@/api/car/carowner'

export default {
  dicts: ['car_plate_type','car_owner_source'],
  data() {
    return {
      visible: false,
      canSubmit: false,
      dataForm: {
        id: '',
        name: '',
        carType: '',
        mobile: '',
        plateNumber: '',
        frontUserId: '',
        createTime: '',
        createBy: '',
        updateTime: '',
        updateBy: '',
        img: '',
        dataSourceType: '',
        collectorId: '',
      },
      dataRule: {
        name: [
          {required: true, message: '车主姓名不能为空', trigger: 'blur'}
        ],

        carType: [
          {required: true, message: '车牌类型不能为空', trigger: 'blur'}
        ],

        mobile: [
          {required: true, message: '手机号不能为空', trigger: 'blur'}
        ],

        plateNumber: [
          {required: true, message: '车牌号不能为空', trigger: 'blur'}
        ],

        frontUserId: [
          {required: true, message: '请选择c端用户', trigger: 'blur'}
        ],

        img: [
          {required: true, message: '车辆照片不能为空', trigger: 'blur'}
        ],

        dataSourceType: [
          {required: true, message: '信息来源不能为空', trigger: 'blur'}
        ],

      /*  collectorId: [
          {required: true, message: '收费员id不能为空', trigger: 'blur'}
        ],*/

      }
    }
  },
  methods: {
    init(id) {
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (id) {
          getObj(id).then(response => {
            this.dataForm = response.data.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm).then(data => {
              this.$notify.success('添加成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    },
    //重置表单
    closeDialog() {
      this.$refs["dataForm"].resetFields()
    },
    //获取c端用户下拉框
    getFrontUser(){

    }
  }
}
</script>
